package com.zabra.zl.ui.inputpwd;

import android.app.Dialog;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zabra.zl.Config;
import com.zabra.zl.R;
import com.zabra.zl.base.BaseActivity;
import com.zabra.zl.base.BaseMvpActivity;
import com.zabra.zl.data.SimpleBean;
import com.zabra.zl.myview.pwdview.OnPasswordInputFinish;
import com.zabra.zl.myview.pwdview.PasswordView;
import com.zabra.zl.ui.inputpwd.contract.InputPwdContract;
import com.zabra.zl.ui.inputpwd.presenter.InputPwdPresenter;
import com.zabra.zl.ui.success.UnbundleBankSuccessActivity;
import com.zabra.zl.utils.DialogUtils;
import com.zabra.zl.utils.MaptoJsonUtils;
import com.zabra.zl.utils.ShowDialogUtils;
import com.zabra.zl.utils.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 输入交易密码
 */
public class InputPwdActivity extends BaseMvpActivity<InputPwdPresenter> implements InputPwdContract.View {


    @BindView(R.id.back)
    LinearLayout back;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.pwd_view)
    PasswordView passview;
    private String resultPwd, strBankid, strMoney;
    private Bundle bundle = new Bundle();
    private Dialog dialog;

    @Override
    public int getLayoutId() {
        return R.layout.activity_input_pwd;
    }

    @Override
    public void initView() {
        mPresenter = new InputPwdPresenter();
        mPresenter.attachView(this);
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            //输入交易密码提现
            strMoney = extras.getString("money");
            strBankid = extras.getString("bankid");
            tvTitle.setText("输入交易密码");
            passview.setHintText("请输入交易密码");
            passview.setOnFinishInput(new OnPasswordInputFinish() {
                @Override
                public void inputFinish() {
                    resultPwd = passview.getStrPassword();
                    keyList.add("debtorId");
                    keyList.add("cardNo");
                    keyList.add("amt");
                    keyList.add("tranPwd");
                    valueList.add(strUserid);
                    valueList.add(strBankid);
                    valueList.add(strMoney);
                    valueList.add(resultPwd);
                    mPresenter.getData(MaptoJsonUtils.Simple(keyList, valueList));
                    keyList.clear();
                    valueList.clear();
                }
            });
        } else {
            //设置或修改交易密码
            tvTitle.setText("输入交易密码");
            passview.setHintText("支付密码不能重复或为连续的数字");
            passview.setOnFinishInput(new OnPasswordInputFinish() {
                @Override
                public void inputFinish() {
                    resultPwd = passview.getStrPassword();
                    bundle.putString("pwd", resultPwd);
                    toClass1(InputPwdActivity.this, InputAginPwdActivity.class, bundle);
                }
            });
        }

    }


    @OnClick(R.id.back)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void showLoading() {
        dialog = DialogUtils.showDialog_progressbar(this);
    }

    @Override
    public void hideLoading() {
        DialogUtils.closeDialog(dialog);
    }

    @Override
    public void onError(Throwable throwable) {
        DialogUtils.closeDialog(dialog);
    }

    @Override
    public void onSuccess(SimpleBean bean) {
        if (bean.getRespCode().equals(Config.SUCCESS)) {
            ToastUtil.showToast(this, bean.getRespDesc());
            finish();
        } else if (bean.getRespCode().equals(Config.TOKENOUT)) {
            ShowDialogUtils.restLoginDialog(this);
        } else if (bean.getRespCode().equals(Config.WAITING)) {
            ToastUtil.showToast(this, bean.getRespDesc());
            finish();
        } else {
            ToastUtil.showToast(this, bean.getRespDesc());
        }
    }
}
